package negocio.Venta;
import negocio.Vehiculo.TransferVehiculo;
import integracion.Cliente.DAOCliente;
import integracion.Factoria.*;
import integracion.Vehiculo.DAOVehiculo;
import integracion.Venta.*;


public class SAVentaImp implements SAVenta {
	

	@Override
	public boolean crearVenta(TransferVenta t) {
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVenta daoVenta = FactDao.newDAOVenta();		
        if(checkTransfer(t))
        	{
            decreaseStock(t);
        	return daoVenta.create(t);
        	}		   
		 return false;
	}

	@Override
	public boolean eliminarVenta (TransferVenta t) {		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVenta daoVenta = FactDao.newDAOVenta();
		if(daoVenta.deletable(t))
			{
			increaseStock(t);
			return daoVenta.delete(t);
			}
		return false;
			
	}

	@Override
	public TransferVenta buscarVenta(int id) {
		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVenta daoVenta = FactDao.newDAOVenta();
		if(daoVenta.searchable(id))
			return daoVenta.readById(id);
		return null;
		}
	
	@Override
	public boolean actualizarVenta(TransferVenta t) { 		
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVenta daoVenta = FactDao.newDAOVenta();
		if(daoVenta.updatable(t))
			{
			TransferVenta aux = buscarVenta(t.getId());
			increaseStock(aux);
			if(checkTransfer(t))
				{
				decreaseStock(t);
			    return daoVenta.update(t);
				}
			else
				decreaseStock(aux);
			}
		return false;				
	}	
	
	private boolean checkTransfer(TransferVenta t)
	{
	FactoriaDAO FactDao = FactoriaDAO.getInstancia();	
	DAOCliente daoCliente = FactDao.newDAOClientes();
	DAOVehiculo daoVehiculo=FactDao.newDAOVehiculo();
	if(!daoCliente.searchable(t.getCliente())) return false;
	int nlineas = t.getNumeroLineasVenta();
	for(int i=0;i<nlineas;i++)
		{
		 if(t.getLineaVenta(i).getNumeroVehiculos()<1)
			 return false;
		 if(t.getLineaVenta(i).getPrecioUdad()<0)
			 return false;
		 if(!daoVehiculo.searchable(t.getLineaVenta(i).getVehiculo())) 
			 return false;		 
		 if(t.getLineaVenta(i).getNumeroVehiculos() >  daoVehiculo.readById(t.getLineaVenta(i).getVehiculo()).getStock())
			 return false;	
		 if(!daoVehiculo.updatable(daoVehiculo.readById(t.getLineaVenta(i).getVehiculo())))
		   return false;
		}
	return true;
	}
		
	private void decreaseStock(TransferVenta t)
	{
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVehiculo daoVehiculo=FactDao.newDAOVehiculo();
		int nlineas = t.getNumeroLineasVenta();
		for(int i=0;i<nlineas;i++)
			{
			TransferVehiculo vehiculo = daoVehiculo.readById(t.getLineaVenta(i).getVehiculo());
			if(daoVehiculo.updatable(vehiculo))
				{	
				vehiculo.setStock(vehiculo.getStock()-t.getLineaVenta(i).getNumeroVehiculos());			
				daoVehiculo.update(vehiculo);
				}
			}					
	}
	
	private void increaseStock(TransferVenta t)
	{
		FactoriaDAO FactDao = FactoriaDAO.getInstancia();
		DAOVehiculo daoVehiculo=FactDao.newDAOVehiculo();
		DAOVenta daoVenta = FactDao.newDAOVenta();
		TransferVenta transferventa = daoVenta.readById(t.getId());
		int nlineas = transferventa.getNumeroLineasVenta();
		for(int i=0;i<nlineas;i++)
		  {
			int idvehiculo = daoVenta.readById(transferventa.getId()).getLineaVenta(i).getVehiculo();
			int stock = daoVenta.readById(transferventa.getId()).getLineaVenta(i).getNumeroVehiculos();
			TransferVehiculo vehiculo = daoVehiculo.readById(idvehiculo);
			if(daoVehiculo.updatable(vehiculo))
				{
				vehiculo.setStock(vehiculo.getStock() + stock);
				daoVehiculo.update(vehiculo);
				}
		  }		
	}
}
	
